This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 8.2.7
Category: Advisory
Analysis Type: Decidable,Single Translation Unit
Rationale
Casting between a pointer and an integer type makes it harder for tools and developers to understand and reason about code behaviour. For example,
pointer tracking within tools may become unreliable when pointers are cast to integers.
Note: casting between pointers and integers may be unavoidable when addressing memory mapped registers or other hardware specific
features. When the advice given in this rule is not followed, the use of std::uintptr_t
or std::intptr_t
is required by
MISRA C++ 2023 Rule 8.2.8 (An object pointer type shall not be cast to an integral type other than std::uintptr_t
or
std::intptr_t
) as these types are guaranteed to be able to represent all possible pointer values.
Example
The following examples violate MISRA C++ 2023 Rule 8.2.8 (An object pointer type shall not be cast to an integral type other than
std::uintptr_t
or std::intptr_t
):
struct S;
void f( S * s )
{
std::intptr_t p = reinterpret_cast< std::intptr_t >( s ); // Non-compliant
std::uint8_t q = reinterpret_cast< std::uint8_t >( s ); // Non-compliant
}
Copyright The MISRA Consortium Limited © 2023